package com.ruoyi.enterprise.controller;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.enterprise.service.IEProfessionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.e_enterprise.EContract;
import com.ruoyi.enterprise.service.IEContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.ruoyi.common.constant.PersonConstants.File_path;

/**
 * 合同Controller
 * 
 * @author JYW
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/enterprise/contract")
public class EContractController extends BaseController
{
    @Autowired
    private IEContractService eContractService;

    @Autowired
    private IEProfessionService professionService;

    /**
     * 查询合同列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(EContract eContract)
    {
        startPage();
        List<EContract> list = eContractService.selectEContractList(eContract).stream()
                .peek(eContract1 -> {
                    eContract1.setRemark(professionService.selectEProfessionById(eContract1.getpId()).getpName());
                })
                .collect(Collectors.toList());

        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contract:export')")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EContract eContract)
    {
        List<EContract> list = eContractService.selectEContractList(eContract);
        ExcelUtil<EContract> util = new ExcelUtil<EContract>(EContract.class);
        util.exportExcel(response, list, "合同数据");
    }

    /**
     * 获取合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eContractService.selectEContractById(id));
    }

    /**
     * 新增合同
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contract:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( @RequestBody EContract eContract) throws IOException {
        eContract.setFilePath(File_path.concat(eContract.getFilePath().substring(9)));
        return toAjax(eContractService.insertEContract(eContract));
    }

    /**
     * 修改合同
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contract:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EContract eContract)
    {
        return toAjax(eContractService.updateEContract(eContract));
    }

    /**
     * 删除合同
     */
    @PreAuthorize("@ss.hasPermi('enterprise:contract:remove')")
    @Log(title = "合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eContractService.deleteEContractByIds(ids));
    }
}
